package com.bey.bow.web.course;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.bey.bow.entity.Course;
import com.bey.bow.entity.CourseDetail;
import com.bey.bow.service.course.CourseDetailService;
import com.bey.bow.service.course.CourseService;

@Controller
@RequestMapping(value = "admin/courseDetail")
public class CourseDetailAdminController {

	private CourseService courseService;

	private CourseDetailService courseDetailService;

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(@RequestParam("courseId") long courseId,
			Model model) {
		model.addAttribute("courseDetail", new CourseDetail());
		model.addAttribute("action", "create");
		model.addAttribute("courseId", courseId);
		return "admin/course/courseDetailForm";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(@RequestParam("courseId") long courseId,
			@ModelAttribute("courseDetail") CourseDetail courseDetail,
			RedirectAttributes redirectAttributes) {

		System.out.println(courseDetail.isViewOpen());

		Course course = courseService.getCourse(courseId);
		courseDetail.setCourse(course);
		course.getCourseDetails().add(courseDetail);
		courseService.saveCourse(course);
		redirectAttributes.addFlashAttribute("message", "创建任务成功");
		return "redirect:/admin/courseDetail/list/" + courseId;
	}

	@RequestMapping(value = "list/{id}")
	public String list(@PathVariable("id") Long id, Model model) {

		Course course = courseService.getCourse(id);

		model.addAttribute("course", course);
		model.addAttribute("courseId", id);

		return "admin/course/courseDetailList";
	}

	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id,
			@RequestParam("courseId") long courseId,
			RedirectAttributes redirectAttributes) {

		courseService.deleteCourseDetail(id, courseId);
		redirectAttributes.addFlashAttribute("message", "删除课程成功");
		return "redirect:/admin/courseDetail/list/" + courseId;
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id,
			@RequestParam("courseId") long courseId, Model model) {
		
		model.addAttribute("courseDetail",courseDetailService.getCourseDetail(id));
		model.addAttribute("action", "update");
		model.addAttribute("courseId", courseId);
		
		return "admin/course/courseDetailForm";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(
			@ModelAttribute("courseDetail") CourseDetail courseDetail,
			@RequestParam("courseId") long courseId,
			RedirectAttributes redirectAttributes) {
		courseDetail.setCourse(courseService.getCourse(courseId));
		courseDetailService.saveCourseDetail(courseDetail);
		redirectAttributes.addFlashAttribute("message", "更新课程成功");
		return "redirect:/admin/courseDetail/list/" + courseId;
	}

	@Autowired
	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}

	@Autowired
	public void setCourseDetailService(CourseDetailService courseDetailService) {
		this.courseDetailService = courseDetailService;
	}

}
